package org.NooLab.glue.storage.persistence;

 
import java.util.Map;
import java.util.SortedMap;
 

import org.NooLab.utilities.files.DFutils;
import org.NooLab.utilities.files.DirectoryContent;
import org.NooLab.utilities.logging.PrintLog;

import org.NooLab.glue.MessageBoard;
import org.NooLab.glue.msgframe.Notifications;
import org.NooLab.glue.storage.MsgBoardDataStorage;




public class PersistenceHandling {

	MessageBoard messageBoard ;
	MsgBoardDataStorage  msgBoardStorage ;
	
	SortedMap<Long, String> openTransactions ;
	
	String homePath="";
	
	PrintLog out = new PrintLog(2,true);
	DFutils fileutil = new DFutils() ;
	DirectoryContent dirlist = new DirectoryContent() ;
	
	public PersistenceHandling( MessageBoard mb){
		
		messageBoard = mb;
		msgBoardStorage = messageBoard.getMsgBoardStorage() ;
		homePath = msgBoardStorage.getHomePath() ;
		
	}

	
	
	@SuppressWarnings("unchecked")
	public void checkForHibernatedResults(){
		String path, dictfile, notesdictfile;
		
		Notifications waitingNotifications=null;
		
		// transactions
		// -> <homePath>/transactData , where files are stored like : ~tdata_tid-25_inid-1867.obj
		// path = msgBoardStorage.getDataStoragePath() ;
		
		
		// notifications
		//  -> <homePath>/transferBuffer ,  where files are stored like : ~rn-25-so.btmp
		path = DFutils.createPath( this.homePath,"transferBuffer/");
		
		/* reading files:
		    - ~resultsnnotes.lst   standardized by     : saveNotificationsDictionary() in MsgBoardDataStorage{}
		    					   which is called by  : handlingSingleNotification in MessageBoard for delayed client-side polling
		    					   													   msgBoardStorage.saveNotificationsDictionary( waitingNotifications ) ;
		    					   which stores        : Notifications waitingNotifications ;
		    					   
		    - ~tstor-open_so.dict  standardized by     : createTransactionDictFileName()
		                           which is called by  : saveParticipantstransactions << handlingTransmissionReplies() in DataConnector
		                           which stores        : openTransactions
		                           which is            : SortedMap<Long, String> openTransactions 
		*/
		
		dictfile = DFutils.createPath( path, "~tstor-open_so.dict") ;
		notesdictfile = DFutils.createPath( path, "~resultsnnotes.lst") ;
		
		// ArrayList<String> files = DirectoryContent.getFileList( "~rn", "btmp", path ) ;
		
		Object obj = msgBoardStorage.readTransactionDataObj(dictfile) ;
		openTransactions = (SortedMap<Long, String>)obj ;
		
		for (Map.Entry<Long, String> entry : openTransactions.entrySet()) {
        	out.print(2, "Key <-> Value :   " + entry.getKey() + " <==> " + entry.getValue());
        }
		// contains pairs like
		// 282=1f7f665e-37f2-49e7-bf01-935947c047da  , which is  <transaction id = private transaction secret> 
		// a corresponding file is used by the instance itself in its private directory (labeled with the basic name of the instance) 
		obj =  msgBoardStorage.readTransactionDataObj(notesdictfile) ;
		
		if (obj!=null){
			try{
				waitingNotifications = (Notifications)obj ;
			}catch(Exception e){}
			
			if (waitingNotifications!=null){
				// int k = waitingNotifications.size();
				
			}
		}
		
		
	}
	
	
	
}
